<template>
  <div class="tab-control">
    <div class="tab-control-item"
         v-for="(item, index) in titles"
         :class="{active: currentIndex === index}"
         @click="itemClick(index)"
    >
      <span>{{item}}</span>
    </div>
  </div>
</template>

<script>
    export default {
        name: "TabControl",
        props: {
            titles: {
                type: Array,
                default() {
                    return []
                }
            }
        },
        data: function () {
            return {
                currentIndex: 0
            }
        },
        methods: {
            // TODO: 点击tabItem时，更换商品数据
            itemClick: function (index) {
                // 1.改变currentIndex
                this.currentIndex = index;

                console.log(typeof this);
                console.log(this);
                console.log("当前点击的为", index, this.titles[index]);

                // 2.发出事件 告诉父组件发生了该点击事件
                this.$emit('itemClick', index)
            }
        }
    }
</script>

<style scoped>
  .tab-control {
    display: flex;
    text-align: center;
    line-height: 40px;
    font-size: 15px;
    background-color: #fff;
    z-index: 8;
  }

  .tab-control-item {
    flex: 1;
  }

  .tab-control-item span {
    padding: 5px;
  }

  .active {
    color: var(--color-high-text);
  }

  .active span {
    border-bottom: 3px solid var(--color-high-text);
  }
</style>
